<?php namespace Admin\Controller;

class TagController extends CommonController{
	protected $db;
	public function __construct(){
		$this->db = new \Common\Model\Tag;
		parent::__construct();
	}
	public function index(){
		//获得所有数据
		$data =$this->db->get();
		View::with("data",$data);
		View::make();
	}
	/*
	 * 添加
	 * 
	 * */
	public function add(){
		if(IS_AJAX){
            if(!$this->db->store()){
				//添加失败
				View::ajax(array('status'=>0,'message'=>$this->db->getError()));
			}else{
				//添加成功
				View::ajax(array('status'=>1,'message'=>'添加成功'));
			}
	  }
		View::make();
	}
	/*
	 * 编辑
	 * */
	 public function edit(){
	 	
	 	if(IS_AJAX){
	 		
	 		if(!$this->db->edit()){
	 			View::ajax(array("status"=>0,"message"=>$this->db->getError()));
	 		}else{
	 			View::ajax(array("status"=>1,"message"=>"编辑成功"));
	 		}
	 	}
		$tid = Q("get.tid",0 ,'intval');
		//获得原来的数据
		$olddata = $this->db->where('tid',$tid)->first();
		View::with("olddata",$olddata);
		View::make();
		
	 }
	 /*
	  * 删除
	  * */
	public function del(){
		$tid = Q("get.tid",0,'intval');
		$this->db->del($tid);
		$this->success("删除成功",U("Tag/index"));
	}
}